/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.utils.middleware;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import org.orm.PersistentException;
import org.orm.PersistentTransaction;
import org.testis.orm.classes.T_bed_deviceabstraction;
import org.testis.orm.classes.T_bed_deviceabstractionCriteria;
import org.testis.orm.classes.T_bed_devicedefinitions;
import org.testis.orm.classes.T_bed_devicedefinitionsCriteria;
import org.testis.orm.classes.T_bed_devicedefinitionsFactory;

/**
 * 
 * @author selcuk
 */
public class DeviceDefinitionDataHandler extends AtofOrmFlyweightFactory {

    private static DeviceDefinitionDataHandler m_reference;

    private DeviceDefinitionDataHandler() {
	loadDeviceDefinitions();
    }

    public static DeviceDefinitionDataHandler newInstance() {
	if (m_reference == null) {
	    m_reference = new DeviceDefinitionDataHandler();
	}
	return m_reference;
    }

    public Collection getDeviceDefinitions() {
	return FLY_WEIGHT_DATA.values();
    }

    public T_bed_devicedefinitions[] getDeviceDefinitionArray() {
	Collection coll = getDeviceDefinitions();
	T_bed_devicedefinitions[] result = new T_bed_devicedefinitions[coll
		.size()];
	coll.toArray(result);
	return result;
    }

    public LinkedList<T_bed_devicedefinitions> getDeviceDefinitionList() {
	Collection col = getDeviceDefinitions();
	return new LinkedList<T_bed_devicedefinitions>(col);
    }

    public HashMap getDeviceDefinitionMap() {
	return new HashMap(FLY_WEIGHT_DATA);
    }

    public int getNumberOfDeviceDefinitions() {
	return FLY_WEIGHT_DATA.size();
    }

    public T_bed_devicedefinitions getDeviceDefinitionById(int id) {
	T_bed_devicedefinitions result = null;
	Object obj = getFlyweight(Integer.valueOf(id));
	if (obj != null) {
	    result = (T_bed_devicedefinitions) obj;
	}
	return result;
    }

    public int addDeviceDefinition(String name, int abstractId) {
	T_bed_devicedefinitions device = createDeviceDefinition(name,
		abstractId);
	int id = 0;
	if (device != null) {
	    id = device.getId();
	    putFlyweight(Integer.valueOf(id), device);
	}
	return id;
    }

    public boolean saveDeviceDefinition(int id, String name, int abstractId) {
	return updateDeviceDefinition(id, name, abstractId);
    }

    private boolean updateDeviceDefinition(int id, String name, int abstractId) {
	boolean result = false;
	try {
	    PersistentTransaction t = openDBConnection().beginTransaction();
	    try {
		T_bed_devicedefinitions obj = getDeviceDefinitionById(id);
		if (obj != null) {
		    T_bed_deviceabstraction abstraction = getDeviceAbstraction(abstractId);
		    obj.setName(name);
		    obj.setAbstract(abstraction);
		    result = obj.save();
		}
		t.commit();
	    } catch (PersistentException ex) {
		t.rollback();
	    } finally {
		closeDBConnection();
	    }
	} catch (PersistentException ex) {
	}
	return result;
    }

    private T_bed_devicedefinitions createDeviceDefinition(String name,
	    int abstractId) {
	T_bed_devicedefinitions result = null;
	try {
	    PersistentTransaction t = openDBConnection().beginTransaction();
	    try {
		T_bed_devicedefinitions obj = new T_bed_devicedefinitions();
		T_bed_deviceabstraction abstraction = getDeviceAbstraction(abstractId);
		obj.setName(name);
		obj.setAbstract(abstraction);
		obj.save();
		t.commit();
		result = obj;
	    } catch (PersistentException ex) {
		t.rollback();
	    } finally {
		closeDBConnection();
	    }
	} catch (PersistentException ex) {
	}
	return result;
    }

    private T_bed_deviceabstraction getDeviceAbstraction(int id) {
	T_bed_deviceabstraction result = null;
	try {
	    T_bed_deviceabstractionCriteria criteria = new T_bed_deviceabstractionCriteria();
	    criteria.id.eq(id);
	    result = criteria.uniqueT_bed_deviceabstraction();
	} catch (PersistentException ex) {
	}
	return result;
    }

    @Override
    protected Object loadFlyweight(Object key) {
	T_bed_devicedefinitions obj = readDeviceDefinitionById((Integer) key);
	return obj;
    }

    private void loadDeviceDefinitions() {
	T_bed_devicedefinitions[] deviceDefs = readDeviceDefinitions();
	if (deviceDefs != null) {
	    for (int i = 0; i < deviceDefs.length; i++) {
		putFlyweight(Integer.valueOf(deviceDefs[i].getId()),
			deviceDefs[i]);
	    }
	}
    }

    private T_bed_devicedefinitions readDeviceDefinitionById(int id) {
	T_bed_devicedefinitions result = null;
	try {
	    T_bed_devicedefinitionsCriteria criteria = new T_bed_devicedefinitionsCriteria();
	    criteria.id.eq(id);
	    result = criteria.uniqueT_bed_devicedefinitions();
	} catch (PersistentException ex) {
	}
	return result;
    }

    private T_bed_devicedefinitions[] readDeviceDefinitions() {
	T_bed_devicedefinitions[] result = null;
	try {
	    result = T_bed_devicedefinitionsFactory
		    .listT_bed_devicedefinitionsByQuery(null, null);
	    if (result == null) {
		result = new T_bed_devicedefinitions[0];
	    }
	} catch (PersistentException ex) {
	}
	return result;
    }
}
